package park.test.util.httpclient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * @ClassName BrowserClient
 * @Description TODO(模拟浏览器)
 * @author Administrator
 * @Date 2016年8月27日 上午9:31:05
 * @version 1.0.0
 */
public class BrowserClient {

	public static void getMethiod(String url, Map<String, String> paramsMap)
			throws ClientProtocolException, IOException {
		CloseableHttpResponse response1 = null;
		InputStream is = null;
		try {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			
			List<NameValuePair> params = new ArrayList();
			for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
				params.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
			}

			String str = EntityUtils.toString(new UrlEncodedFormEntity(params, Consts.UTF_8));
			HttpGet httpGet = new HttpGet(url + "?" + str);
			System.out.println(url + "?" + str);

			response1 = httpclient.execute(httpGet);

			// 得到响应体
			HttpEntity entity = response1.getEntity();
			StatusLine statusline=response1.getStatusLine();
			System.out.println("status="+statusline.toString());
			if (entity != null) {
				is = entity.getContent();
				// 转换为字节输入流
				BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));
				String body = null;
				while ((body = br.readLine()) != null) {
					System.out.println(body);
				}
			}
		} finally {
			if(response1!=null){
				response1.close();
			}
			

		}
	}
}
